package com.cxrj.zhsq.interfaceManage.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cxrj.zhsq.propertyManage.service.ReleaseService;

@Controller
@RequestMapping(value = "/interface")
public class InterfaceReleaseController {

	@Resource
	private ReleaseService releaseService;

	/**
	 * 添加找物业
	 */
	@SuppressWarnings("static-access")
	@RequestMapping(value = "insertRelease", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> insertSecretary(String userId, String appContent, @RequestParam(required = false, value = "appPicture[]") String[] appPicture, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String pics = "";
			Base64 base64 = new Base64();
			if (appPicture != null) {
				for (int i = 0; i < appPicture.length; i++) {
					if (i == 0) {
						continue;
					}
					int delLength = appPicture[i].indexOf(',') + 1;
					int begin = appPicture[i].indexOf('/') + 1;
					int end = appPicture[i].indexOf(';');
					byte[] b = base64.decodeBase64(appPicture[i].substring(delLength));
					/* 文件保存路径 */
					String fileName = UUID.randomUUID().toString() + "." + appPicture[i].substring(begin, end);
					String urlStr = request.getSession().getServletContext().getRealPath("/") + ".." + File.separator + "upload/zhsq_xg/" + fileName;
					OutputStream os = new FileOutputStream(new File(urlStr));
					os.write(b);
					os.flush();
					os.close();
					pics += "★" + fileName;
				}
			}
			map = releaseService.insertRelease(userId, appContent, pics);
		} catch (Exception e) {
			map.put("result", "0");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 分页查询找书记
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectRelease", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectRelease(String status, String userAccount, String dataIndex, String pageCount, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = releaseService.selectRelease(status, userAccount, Integer.parseInt(dataIndex), Integer.parseInt(pageCount));
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}

		return resultMap;
	}

	/**
	 * 根据id查询找书记
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectReleaseId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectReleaseId(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = releaseService.selectById(id, request);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}

		return resultMap;
	}
}
